package com.manor.main.activity;

import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzy.okgo.model.Response;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.manor.common.CommonAppConfig;
import com.manor.common.Constants;
import com.manor.common.activity.AbsActivity;
import com.manor.common.http.HttpCallback;
import com.manor.common.http.JsonBean;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.StringUtil;
import com.manor.common.utils.ToastUtil;
import com.manor.common.utils.WordUtil;
import com.manor.main.R;
import com.manor.main.adapter.VisitorAdapter;
import com.manor.main.bean.VisitorBean;
import com.manor.main.http.MainHttpUtil;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 访客记录
 */
@Route(path = RouteUtil.PC_Visitor)
public class VisitorRecordActivity extends AbsActivity {
    int page = 0;
    private List<VisitorBean> visitorBean;
    RecyclerView mRecyclerView;
    VisitorAdapter mAdapter;
    private SmartRefreshLayout srlFollow;
    private String strTotal;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_visitor;
    }

    @Override
    protected void main() {
        ARouter.getInstance().inject(this);

        strTotal = getIntent().getStringExtra(Constants.FROM);

        if (StringUtil.isNotEmpty(strTotal)){
            ((TextView) findViewById(R.id.tvTotal)).setText("（历史" + strTotal + "人）");
        }

        setTitle(WordUtil.getString(R.string.visitors_record));
//        ImmersionBar.with(this)
//                .statusBarColor(R.color.main_bg)
//                .init();
        srlFollow = findViewById(R.id.srl_follow);
        mRecyclerView = findViewById(R.id.rc_listview);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));

        srlFollow.setEnableLoadMore(true);
        srlFollow.setEnableRefresh(true);

        srlFollow.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                setData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                setData(true);
            }
        });
        mAdapter = new VisitorAdapter(R.layout.rv_visitor_item, null);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                UserCenterActivity.Companion.forward(VisitorRecordActivity.this, mAdapter.getItem(position).getV_id());
            }
        });


        setData(true);
    }

    private void setData(final boolean istype) {
        if (istype) {
            page = 1;
        } else {
            page++;
        }
        MainHttpUtil.getGetVisitor(CommonAppConfig.getInstance().getUid(), CommonAppConfig.getInstance().getToken(),page + "", new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String info) {
                if (code == 0 && info != null && StringUtil.isNotEmpty(info)) {
                    //tvTotal


                    visitorBean = JSON.parseArray(info, VisitorBean.class);
                    //   WithdrawalAccountBean withdrawalAccountBean = mGson.fromJson(info[0], WithdrawalAccountBean.class);
                    if (istype) {
                        mAdapter.setNewInstance(visitorBean);

                        srlFollow.finishRefresh();
                        //  withdrawalAccountBeans.clear();
                    } else {
                        mAdapter.addData(visitorBean);
                        srlFollow.finishLoadMore();
                    }


                } else {
                    if (istype && null != visitorBean && visitorBean.size() > 0) {
                        mAdapter.notifyDataSetChanged();
                    }
                    srlFollow.finishLoadMore();
                    srlFollow.finishRefresh();
                    ToastUtil.show(msg);

                }

                if (mAdapter.getData() == null || mAdapter.getData().isEmpty()) {
                    mAdapter.setEmptyView(R.layout.view_empty_default);
                }
            }

            @Override
            public void onError(Response<JsonBean> response) {
                super.onError(response);
                srlFollow.finishLoadMore();
                srlFollow.finishRefresh();
                if (mAdapter.getData() == null || mAdapter.getData().isEmpty()) {
                    mAdapter.setEmptyView(R.layout.view_empty_default);
                }
            }
        });

    }
}
